package com.cms.controller;

import com.cms.bean.Middle;
import com.cms.bean.Move;
import com.cms.bean.Typea;
import com.cms.mapper.UsersMapper;
import com.cms.service.MoveService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class MoveController {
    @Resource
    private MoveService moveService;

    @RequestMapping("list1")
    public String list(Model model,
                       @RequestParam(defaultValue = "1") Integer pageNum
                     /*  @RequestParam("mname")String mname*/

    ) {
        Integer pageSize = 3;
        PageHelper.startPage(pageNum, pageSize);
        Map<String, Object> map = new HashMap<String, Object>();
        List<Move> moves = moveService.SelectMove(map);
        PageInfo<Move> slist = new PageInfo<>(moves);
        /*map.put("mname",mname);*/
        model.addAttribute("slist", slist);
      /*  model.addAttribute("mname", mname);*/
        return "list1";
    }

    @RequestMapping("del")
    @ResponseBody
    public int DelMove(@RequestParam("mid") Integer mid) {
        int i = moveService.DeleteMove(mid);
        if (i > 0) {
            moveService.DeleteMiddle(mid);
        }
        return i;
    }

    @RequestMapping("add")
    @ResponseBody
    public String AddMove(Move move, int ke[]) {
        int i = moveService.AddMove(move);
        if (i > 0) {
            for (int i1 : ke) {
                Middle middle = new Middle();
                middle.setMid(move.getMid());
                moveService.AddMiddle(middle);

            }
            return "redirect:list";
        }
        return "add";
    }
    @RequestMapping("cate")
    @ResponseBody
    public  List<Typea>typeaList(){
        List<Typea> typeas = moveService.SelectTypea();
        return typeas;
    }
}